﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// add
using System.IO;
using System.Reflection;
using FormBaseLib;

namespace FormBase
{
    public partial class FMain : Form
    {
        //private CProject mProject = Program.gProject;
        private Bitmap mBMPBackground;
        private delegate void ShowMsgEventHandler(string sMsg, Color color1);

        public FMain()
        {
            InitializeComponent();

        }

        private void FMain_Load(object sender, EventArgs e)
        {
            string s1;

            Text = "Main menu";
            //WindowState = Properties.Settings.Default.WindowState;
            //Left = Properties.Settings.Default.Left;
            //Top = Properties.Settings.Default.Top;
            //Width = Properties.Settings.Default.Width;
            //Height = Properties.Settings.Default.Height;
            Left = CProject.mCProjectRun.mRow.FLeft;
            Top =  CProject.mCProjectRun.mRow.FTop;
            Width =  CProject.mCProjectRun.mRow.FWidth;
            Height = CProject.mCProjectRun.mRow.FHeight;

            if (Left < 0) 
                Left = 0;
            if (Top < 0)
                Top = 0;
            if (Width < 200) 
                Width = 200;
            if (Height < 150) 
                Height = 150;

            StartPosition = FormStartPosition.CenterScreen;
            lblStatus.DoubleClickEnabled = true;

            switch (CProject.mEnvironment)
            {
                case enumEnvironment.Development:
                    lblVersion.Text = "| V " + CProject.mApp.msAssemblyVersion + " D";
                    break;
                case enumEnvironment.Testing:
                    lblVersion.Text = "| V " + CProject.mApp.msAssemblyVersion + " T";
                    break;

                default:
                    lblVersion.Text = "| V " + CProject.mApp.msAssemblyVersion;
                    break;
            }

            s1 = @".\App_Code\BackGround.bmp";
            if (File.Exists(s1))
            {
                mBMPBackground = new Bitmap(s1);
                BackgroundImage = mBMPBackground;
                BackgroundImageLayout = ImageLayout.Stretch;
            }
            myRefresh();

            //mProject.ShowForm(typeof(FPGM0010));

            //s1 = "v " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + " Ready.";
            ShowMsg("Ready");
            ShowMsg(CProject.mCProjectXML.mRow.FProjectName);
        }
        public void ActionBegin(string sSubject)
        {
            CProject.mApp.mAction.BeginAction(sSubject);
            ShowMsg(sSubject);
        }
        public void ActionSuccess(string sMsg)
        {
            ShowMsg(sMsg);
            CProject.mApp.mAction.EndSuccess(sMsg);
        }
        public void ActionSuccessPopup(string sMsg)
        {
            ShowMsg(sMsg);
            CProject.mApp.mAction.EndSuccess(sMsg);
            CProject.mApp.PopupMsg(sMsg);
        }
        public void ActionError(string sMsg)
        {
            ShowMsgError(sMsg);
            CProject.mApp.mAction.EndError(sMsg);
        }
        public void ActionErrorPopup(string sMsg)
        {
            ShowMsgError(sMsg);
            CProject.mApp.mAction.EndError(sMsg);
            CProject.mApp.PopupError(sMsg);
        }

        private void myRefresh()
        {
        }

        private void lblStatus_DoubleClick(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FActionLog));
        }
        private void OnShowMsg(string sMsg, Color color1)
        {
            lblStatus.Text = sMsg;
            if (lblStatus.ForeColor != color1)
                lblStatus.ForeColor = color1;
        }
        public void ShowMsg(string sMsg)
        {
            CProject.mApp.mAction.AppendMsgDebug(sMsg);
            Invoke(new ShowMsgEventHandler(OnShowMsg), new object[] { sMsg, SystemColors.ControlText });
        }
        public void ShowMsgError(string sMsg)
        {
            CProject.mApp.mAction.AppendMsgDebug(sMsg);
            Invoke(new ShowMsgEventHandler(OnShowMsg), new object[] { sMsg, Color.Red });
        }

        private void FMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Properties.Settings.Default.WindowState = WindowState;
            //Properties.Settings.Default.Left = Left;
            //Properties.Settings.Default.Top = Top;
            //Properties.Settings.Default.Width = Width;
            //Properties.Settings.Default.Height = Height;

            CProject.mCProjectRun.mRow.FLeft = Left;
            CProject.mCProjectRun.mRow.FTop = Top;
            CProject.mCProjectRun.mRow.FWidth = Width;
            CProject.mCProjectRun.mRow.FHeight = Height;
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            myClose();
        }
        private void myClose()
        {
            Close();
            //CProject.End();
        }

        private void pGM0010ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0010));
        }

        private void pGM0020ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0020));

        }

        private void optionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FOption));
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FAbout));

        }

        private void pGM0030ScannedByRS232ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0030));

        }

        private void PGM0040ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0040));

        }

        private void tCPClientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0050));

        }

        private void tCPServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0060));

        }

        private void xMLBrowseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FBRW0010));
        }

        private void xMLUsageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0070));

        }

        private void fileSystemUploadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0080));

        }

        private void lblStatus_Click(object sender, EventArgs e)
        {
            // keep for IDE click.
        }

        private void webBrowserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0090));

        }

        private void menuTest1_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FTest1));

        }

        private void menuToDo1_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FToDo1));
        }

        private void about2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FAboutListView));

        }

        private void enumDictFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0100));
        
        }

        private void menuPGM0110_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0110));
           
        }

        private void googleMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0120));

        }

        private void menuLocalDB_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0130));

        }

        private void menuTempTest_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FTemp));

        }

        private void menuSQLServerAndExcel_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FPGM0140));

        }

        private void xMLMaintainToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FMNT0010));

        }

        private void browseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CProject.gShowForm(typeof(FBRW0010));
        }

    }
}
